#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::face_init()
{

}

void MainWindow::face_recognition()
{
    this->video_timer->stop();
    ui->pushButton_affirm->setText("识别中");
    ui->pushButton_affirm->setDisabled(true);//禁用确认按钮并显示状态

    QString url = QString(settings->value("GUARD/face_search_url").toString());
    qDebug() << url;
    Http *http = new Http(url, Http::POST);
    connect(http->getAccessManager(),
            &QNetworkAccessManager::finished,
            this,
            &MainWindow::face_recognitionCallback);

    /*对图片进行解码base64*/
    video_v4l2->get_getframe(video_buffer,&video_size);
    video_v4l2->yuv422torgb24((unsigned char*)video_buffer,(unsigned char*)video_rgbbuf,640,480);

    //QImage(uchar *data, int width, int height, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr)
    QImage image = QImage((unsigned char*)video_rgbbuf,640,480,QImage::Format_RGB888);

    /*image change to base64*/
    QByteArray ba;
    QBuffer buf(&ba);
    image.save(&buf, "PNG");
    QByteArray hexed = ba.toBase64();
    QString b64str = QString::fromLatin1(hexed);
    buf.close();

//    QStringList str_list = b64str.split(",");
//    b64str = str_list.at(1);
    b64str = b64str.mid(b64str.indexOf(','));//去除image文件头
    QString data = QString("{\"image\":\"%1\",\"image_type\":\"%2\",\"group_id_list\":\"%3\"}")
            .arg(b64str)
            .arg("BASE64")
            .arg("gec");

//    qDebug() << b64str;
    http->setData(data);
    http->send();

}

void MainWindow::face_recognitionCallback(QNetworkReply *reply)
{
    this->video_timer->start();                 //恢复视频刷新
    ui->pushButton_affirm->setText("确认");
    ui->pushButton_affirm->setDisabled(false);  //禁用确认按钮并显示状态

    QJsonObject jsonObject = Http::checkHttpResponseError(reply);
    if(jsonObject.isEmpty())
        return;
    qDebug() << QString(QJsonDocument(jsonObject).toJson(QJsonDocument::Compact));
    if(jsonObject.value("error_code").toInt() != 0)
    {
        QString errStr = "error_code: " + QString(jsonObject.value("error_code").toInt()) +
                " " + jsonObject.value("error_msg").toString();
        QMessageBox::critical(this, "ERROR", errStr);
        qCritical() << errStr;
        return;
    }

    QJsonObject jsonObjectResult = jsonObject.value("result").toObject();
    QJsonValue jsonValueUserList = jsonObjectResult.value("user_list");
    QJsonArray jsonArrayUserList = jsonValueUserList.toArray();
    QJsonValue jsonValuetUserListNo_1 = jsonArrayUserList.at(0);
    QJsonObject jsonObjectUserListNo_1 = jsonValuetUserListNo_1.toObject();
    QJsonValue jsonValueNo_1Score = jsonObjectUserListNo_1.value("score");
    if(jsonValueNo_1Score.isUndefined())
    {
        qCritical() << "score undefined.";
        return;
    }
    double score = jsonValueNo_1Score.toDouble();
    if(score < face_threshold)
    {
        QMessageBox::critical(this, "ERROR", "人脸识别错误");
    }
    else
    {
        QMessageBox::information(this, "SUCCEED", QString("你好，%1，欢迎光临")
                                 .arg(jsonObjectUserListNo_1.value("user_id").toString()));
    }
    this->setCursor(Qt::BlankCursor);//隐藏鼠标指针
}
